package com.hll.service.user;

import com.hll.mapper.UserMapper;
import com.hll.pojo.User;
import com.hll.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author: Mr.Hu
 * @create: 2021-03-07 18:09
 */
public class UserServiceImpl implements UserService {

  @Override
  public User login(String userCode, String password) {
    User user = null;
    try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      user = userMapper.getLoginUser(userCode, password);
      sqlSession.close();
    }
    return user;
  }

  @Override
  public boolean updatePwd(int id, String pwd) {
    boolean flag = false;
    try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)) {
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      flag = userMapper.updatePwd(id, pwd) > 0;
      sqlSession.close();
    }
    return flag;
  }

  @Override
  public int getUserCount(String userName, Integer role) {
    int count = 0;
    try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      count = userMapper.getUserCount(userName, role);
      sqlSession.close();
    }
    return count;
  }

  @Override
  public List<User> getUserList(
      String userName, Integer role, final Integer currentPageNo, final Integer pageSize) {
    List<User> userList = null;
    try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      userList = userMapper.getUserList(userName, role, currentPageNo, pageSize);
      sqlSession.close();
    }
    return userList; // 要被气死了,写成了return 0
  }
}
